package com.mxbbx.wdn.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.mxbbx.wdn.database.DBUtil;
import com.mxbbx.wdn.domain.medical.Data;
import com.mxbbx.wdn.domain.medical.Medical;
import com.mxbbx.wdn.domain.vo.Page;

/**
 * 医疗机构管理Dao类
 * 
 * @author 
 * 
 */
public class MedicalDao extends BaseDao {
	private static final String SQL_SELECT_INSTCODE = "select * from medical where instCode=?";

	@Override
	protected Medical handle(ResultSet rs) throws SQLException {
		try {
			Medical medical = new Medical();
			medical.setInstCode(rs.getString("instCode"));
			medical.setOrgaCode(rs.getString("orgaCode"));
			medical.setInstName(rs.getString("instName"));
			medical.setRegCode(rs.getString("regCode"));
			medical.setAreaCode(rs.getString("areaCode"));
			medical.setSubjection(new Data(rs.getString("subjection"), "02"));
			medical.setInstGrade(new Data(rs.getString("instGrade"), "06"));
			medical.setSbdd(new Data(rs.getString("sbdd"), "04"));
			medical.setApprove(new Data(rs.getString("approve"), "04"));
			medical.setEconomy(new Data(rs.getString("economy"), "01"));
			medical.setHygieneBig(new Data(rs.getString("hygieneBig"), "03"));
			medical.setHygieneSmall(new Data(rs.getString("hygieneSmall"),
					"0301"));
			medical.setUnit(rs.getString("unit"));
			medical.setOpenDate(rs.getString("openDate"));
			medical.setRepresent(rs.getString("represent"));
			medical.setFunds(rs.getDouble("funds"));
			return medical;

		} catch (Exception e) {
			// e.printStackTrace();
			throw new SQLException("结果集转换为对象失败:" + e.getMessage(), e);
		}

	}

	/**
	 * 查找对应的医疗机构记录并转为对象实例
	 * 
	 * @param illCode
	 * @return
	 * @throws SQLException
	 */
	public Medical get(String instCode) throws SQLException {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			conn = DBUtil.getConn();
			pstmt = conn.prepareStatement(SQL_SELECT_INSTCODE);
			int index = 1;
			pstmt.setString(index, instCode);
			rs = pstmt.executeQuery();
			return handle(rs);
		} catch (SQLException e) {
			throw new SQLException("读取instCode记录失败", e);
		}finally {
			DBUtil.close(rs, pstmt, conn);
		}
	}

	public List<Medical> queryObjects(String sql, Object[] params)
			throws SQLException {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		List<Medical> results = new ArrayList<Medical>();
		try {
			conn = DBUtil.getConn();
			pstmt = conn.prepareStatement(sql);
			int index = 1;
			if (params != null) {
				for (Object param : params) {
					if (param instanceof String) {
						pstmt.setString(index++, (String) param);
					}
					if (param instanceof Integer) {
						pstmt.setInt(index++, (Integer) param);
					}
					if (param instanceof Float) {
						pstmt.setFloat(index++, (Float) param);
					}
				}

			}
			rs = pstmt.executeQuery();
			while (rs.next()) {
				Medical medical = handle(rs);
				results.add(medical);
			}
			return results;
		} catch (SQLException e) {
			throw new SQLException("执行SQL[" + sql + "]失败", e);
		}finally {
			DBUtil.close(rs, pstmt, conn);
		}
	}

	/**
	 * 分页
	 * 
	 * @param 如
	 *            select * from medical where instCode=?
	 * @param params
	 * @param page
	 * @param size
	 * @return
	 * @throws SQLException
	 */
	public Page queryOjectsByPage(String sql, Object[] params, int pageNo,
			int size) throws SQLException {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		Page page = null;
		List<Medical> datas = new ArrayList<Medical>();
		try {
			int start = (pageNo - 1) * size;
			conn = DBUtil.getConn();
			pstmt = conn.prepareStatement(sql + " limit " + start + "," + size);
			int index = 1;
			if (params != null) {
				for (Object param : params) {
					if (param instanceof String) {
						pstmt.setString(index++, (String) param);
					}
					if (param instanceof Integer) {
						pstmt.setInt(index++, (Integer) param);
					}
					if (param instanceof Float) {
						pstmt.setFloat(index++, (Float) param);
					}
				}
			}
			rs = pstmt.executeQuery();
			while (rs.next()) {
				Medical medical = handle(rs);
				datas.add(medical);
			}
			// 统计SQL对应记录数
			int total = count(sql, params);
			page = new Page(total, pageNo, size, datas);
			return page;
		} catch (SQLException e) {
			throw new SQLException("执行SQL[" + sql + "]失败", e);
		}

	}
}
